package org.eclipse.football;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;

import org.junit.Test;

public class LeagueFileIO {

	@Test
	public void test() {
		// Create a league
		League league = new League("does_not_exist.txt");
		
		// Create 2 teams
		Team team1 = new Team("team1", "user1");
		Team team2 = new Team("team2", "user2");
		
		// Create 2 offensive players, 2 defensive players, and 2 kickers
		OffPlayer off_player1 = new OffPlayer("off_player1", "team");
		OffPlayer off_player2 = new OffPlayer("off_player2", "team");
		DefPlayer def_player1 = new DefPlayer("def_player1", "team");
		DefPlayer def_player2 = new DefPlayer("def_player2", "team");
		Kicker kicker1 = new Kicker("kicker1", "team");
		Kicker kicker2 = new Kicker("kicker2", "team");
		
		// Add stats to all of the players
		off_player1.setPass_yds(100);
		off_player1.setPass_tds(1);
		off_player1.setPicks(1);
		off_player2.setRush_yds(200);
		off_player2.setRush_tds(2);
		off_player2.setRet_tds(1);
		off_player2.setFumbles(1);
		def_player1.setDef_tds(1);
		def_player1.setBlocks(1);
		def_player1.setSacks(2);
		def_player1.setSafeties(1);
		def_player2.setPa(32);
		def_player2.setPicks(2);
		def_player2.setFumbles(2);
		def_player2.setYa(200);
		kicker1.setPat(10);
		kicker2.setFg(2);
		kicker2.setMisses(2);
		
		// Add 1 of each player to each team
		team1.addOffPlayer(off_player1);
		team1.addDefPlayer(def_player1);
		team1.addKicker(kicker1);
		team2.addOffPlayer(off_player2);
		team2.addDefPlayer(def_player2);
		team2.addKicker(kicker2);
		
		// Add both teams to the league
		league.addTeam(team1);
		league.addTeam(team2);
		
		// Write the state of the league out to a file
		league.writeState("test.txt");
		
		// Create a new league from the output file
		League new_league = new League("test.txt");
		
		// Make sure number of teams is the same in each league
		assertEquals(league.getTeams().size(), new_league.getTeams().size());
		
		// Get teams from second league
		ArrayList<Team> new_teams = new_league.getTeams();
		
		// Get first and second team 
		Team new_team1 = new_teams.get(0);
		Team new_team2 = new_teams.get(1);
		
		// Make sure names for two teams are the same in each league
		assertEquals(new_team1.getName(), team1.getName());
		assertEquals(new_team2.getName(), team2.getName());
		
		// Make sure the the two teams have the same number of offensive players,
			// defensive players, and kickers in both leagues
		assertEquals(new_team1.getOff_players().size(), team1.getOff_players().size());
		assertEquals(new_team1.getDef_players().size(), team1.getDef_players().size());
		assertEquals(new_team1.getKickers().size(), team1.getKickers().size());
		assertEquals(new_team2.getOff_players().size(), team2.getOff_players().size());
		assertEquals(new_team2.getDef_players().size(), team2.getDef_players().size());
		assertEquals(new_team2.getKickers().size(), team2.getKickers().size());
		
		// Get the offensive players, defensive players, and kickers from the new league
		OffPlayer new_off_player1 = new_team1.getOff_players().get(0);
		OffPlayer new_off_player2 = new_team2.getOff_players().get(0);
		DefPlayer new_def_player1 = new_team1.getDef_players().get(0);
		DefPlayer new_def_player2 = new_team2.getDef_players().get(0);
		Kicker new_kicker1 = new_team1.getKickers().get(0);
		Kicker new_kicker2 = new_team2.getKickers().get(0);
		
		// Make sure the names of the corresponding players are the same in each of the leagues
		assertEquals(new_off_player1.getName(), off_player1.getName());
		assertEquals(new_off_player2.getName(), off_player2.getName());
		assertEquals(new_def_player1.getName(), def_player1.getName());
		assertEquals(new_def_player2.getName(), def_player2.getName());
		assertEquals(new_kicker1.getName(), kicker1.getName());
		assertEquals(new_kicker2.getName(), kicker2.getName());
		
		// Make sure the team names the corresponing players are same in each league
		assertEquals(new_off_player1.getTeam(), off_player1.getTeam());
		assertEquals(new_off_player2.getTeam(), off_player2.getTeam());
		assertEquals(new_def_player1.getTeam(), def_player1.getTeam());
		assertEquals(new_def_player2.getTeam(), def_player2.getTeam());
		assertEquals(new_kicker1.getTeam(), kicker1.getTeam());
		assertEquals(new_kicker2.getTeam(), kicker2.getTeam());
		
		// Make sure stats are same for corresponding offensive players in each league
		assertEquals(new_off_player1.getPass_yds(), off_player1.getPass_yds());
		assertEquals(new_off_player2.getPass_yds(), off_player2.getPass_yds());
		assertEquals(new_off_player1.getPass_tds(), off_player1.getPass_tds());
		assertEquals(new_off_player2.getPass_tds(), off_player2.getPass_tds());
		assertEquals(new_off_player1.getPicks(), off_player1.getPicks());
		assertEquals(new_off_player2.getPicks(), off_player2.getPicks());
		assertEquals(new_off_player1.getRush_yds(), off_player1.getRush_yds());
		assertEquals(new_off_player2.getRush_yds(), off_player2.getRush_yds());
		assertEquals(new_off_player1.getRush_tds(), off_player1.getRush_tds());
		assertEquals(new_off_player2.getRush_tds(), off_player2.getRush_tds());
		assertEquals(new_off_player1.getRec_yds(), off_player1.getRec_yds());
		assertEquals(new_off_player2.getRec_yds(), off_player2.getRec_yds());
		assertEquals(new_off_player1.getRec_tds(), off_player1.getRec_tds());
		assertEquals(new_off_player2.getRec_tds(), off_player2.getRec_tds());
		assertEquals(new_off_player1.getRet_tds(), off_player1.getRet_tds());
		assertEquals(new_off_player2.getRet_tds(), off_player2.getRet_tds());
		assertEquals(new_off_player1.getFumbles(), off_player1.getFumbles());
		assertEquals(new_off_player2.getFumbles(), off_player2.getFumbles());
	
		// make sure stats are same for corresponding defensive players in each league
		assertEquals(new_def_player1.getBlocks(), def_player1.getBlocks());
		assertEquals(new_def_player2.getBlocks(), def_player2.getBlocks());
		assertEquals(new_def_player1.getDef_tds(), def_player1.getDef_tds());
		assertEquals(new_def_player2.getDef_tds(), def_player2.getDef_tds());
		assertEquals(new_def_player1.getFumbles(), def_player1.getFumbles());
		assertEquals(new_def_player2.getFumbles(), def_player2.getFumbles());
		assertEquals(new_def_player1.getPa(), def_player1.getPa());
		assertEquals(new_def_player2.getPa(), def_player2.getPa());
		assertEquals(new_def_player1.getPicks(), def_player1.getPicks());
		assertEquals(new_def_player2.getPicks(), def_player2.getPicks());
		assertEquals(new_def_player1.getSacks(), def_player1.getSacks());
		assertEquals(new_def_player2.getSacks(), def_player2.getSacks());
		assertEquals(new_def_player1.getSafeties(), def_player1.getSafeties());
		assertEquals(new_def_player2.getSafeties(), def_player2.getSafeties());
		assertEquals(new_def_player1.getYa(), def_player1.getYa());
		assertEquals(new_def_player2.getYa(), def_player2.getYa());
		
		// Make sure stats are same for corresponding kickers in each league
		assertEquals(new_kicker1.getFg(), kicker1.getFg());
		assertEquals(new_kicker2.getFg(), kicker2.getFg());
		assertEquals(new_kicker1.getMisses(), kicker1.getMisses());
		assertEquals(new_kicker2.getMisses(), kicker2.getMisses());
		assertEquals(new_kicker1.getPat(), kicker1.getPat());
		assertEquals(new_kicker2.getPat(), kicker2.getPat());
	}

}
